{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if an Array Is Consecutive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isConsecutive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查数组是否连贯"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，如果 <code>nums</code> 是一个 <strong>连贯数组</strong> ，则返回 <code>true</code> ，否则返回 <code>false</code> 。</p>\n",
    "\n",
    "<p><span style=\"\">如果数组包含 </span><code>[x, x + n - 1]</code><span style=\"\"> 范围内的所有数字（包括 <code>x</code> 和 <code>x + n - 1</code> ），则该数组为连贯数组；其中</span> <code>x</code><span style=\"\"> 是数组中最小的数，</span><code>n</code> <span style=\"\">是数组的长度。</span></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,3,4,2]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "最小值是 1 ，数组长度为 4 。\n",
    "范围 [x, x + n - 1] 中的所有值都出现在 nums 中：[1, 1 + 4 - 1] = [1, 4] = (1, 2, 3, 4) 。\n",
    "因此，nums 是一个连贯数组。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,3]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：\n",
    "</strong>最小值是 1 ，数组长度为 2 。 \n",
    "范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中：[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。 \n",
    "因此，nums 不是一个连贯数组。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [3,5,4]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "最小值是 3 ，数组长度为 3 。\n",
    "范围 [x, x + n - 1] 中的所有值都出现在 nums 中：[3, 3 + 3 - 1] = [3, 5] = (3，4，5) 。\n",
    "因此，nums 是一个连贯数组。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<strong>提示：</strong>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-an-array-is-consecutive](https://leetcode.cn/problems/check-if-an-array-is-consecutive/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-an-array-is-consecutive](https://leetcode.cn/problems/check-if-an-array-is-consecutive/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,3,4,2]', '[1,3]', '[3,5,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        for i,j in pairwise(nums):\n",
    "            if j!=i+1:return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        # nums.sort()\n",
    "        # s = nums[0]\n",
    "        # res = True\n",
    "        # n = len(nums)\n",
    "        # for i in range(1, n):\n",
    "        #     s += 1\n",
    "        #     if nums[i] != s:\n",
    "        #         res = False\n",
    "        #         break\n",
    "        # return res\n",
    "        minv = min(nums)\n",
    "        n = len(nums)\n",
    "        arr = [0] * n\n",
    "        res = True\n",
    "        for i in nums:\n",
    "            if i - minv > n - 1:\n",
    "                res = False\n",
    "                break\n",
    "            arr[i - minv] = 1\n",
    "        if res:\n",
    "            return sum(arr) == n\n",
    "        else:\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        minv = min(nums)\n",
    "        maxv = minv + len(nums)-1\n",
    "        nums.sort()\n",
    "        if nums[-1] != maxv:\n",
    "            return False\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] - nums[i]!=1:       \n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        return all(1 == a - b for a, b in zip(nums[1:], nums[:-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        s = nums[0]\n",
    "        res = True\n",
    "        n = len(nums)\n",
    "        for i in range(1, n):\n",
    "            s += 1\n",
    "            if nums[i] != s:\n",
    "                res = False\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# my:138/140---pass(遍历) \n",
    "# [0,3,0,3] not pass\n",
    "# class Solution:\n",
    "#     def isConsecutive(self, nums: List[int]) -> bool:\n",
    "#         n=len(nums)\n",
    "#         mi,mx=float(\"inf\"),float(\"-inf\")\n",
    "#         for num in nums:\n",
    "#             mi=min(mi,num)\n",
    "#             mx=max(mx,num)\n",
    "#         return mx==mi+n-1\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        nums=sorted(nums)\n",
    "        for i in range(1,n):\n",
    "            if nums[i]!=nums[i-1]+1:\n",
    "                return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if len(set(nums)) != n:\n",
    "            return False\n",
    "        \n",
    "        nums.sort()\n",
    "    \n",
    "        return nums[0] + n - 1 == nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if len(set(nums)) != n:\n",
    "            return False\n",
    "        \n",
    "        nums.sort()\n",
    "    \n",
    "        return nums[0] + n - 1 == nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        m=len(set(nums))\n",
    "        n=len(nums)\n",
    "        if m<n:\n",
    "            return False\n",
    "        if max(nums)-min(nums)+1==n:\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        return max(nums) - min(nums) + 1 == len(nums) and len(set(nums)) == len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if len(set(nums)) != n:\n",
    "            return False\n",
    "        \n",
    "        \n",
    "    \n",
    "        return min(nums) + n - 1 == max(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        return nums[-1] - nums[0] + 1 == n and len(set(nums)) == n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        numsSet = set(nums)\n",
    "        minNum = min(nums)\n",
    "        for num in range(minNum, minNum + len(nums)):\n",
    "            if num not in numsSet:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        return max(nums) - min(nums) + 1 == len(nums) and len(set(nums)) == len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if len(set(nums)) != n:\n",
    "            return False\n",
    "    \n",
    "        return min(nums) + n - 1 == max(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        return max(nums) - min(nums) + 1 == len(nums) and len(set(nums)) == len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        if not nums:\n",
    "            return False\n",
    "        \n",
    "        min_val = min(nums)\n",
    "        max_val = max(nums)\n",
    "        n = len(nums)\n",
    "        \n",
    "        # 检查最大值和最小值是否满足条件\n",
    "        if max_val != min_val + n - 1:\n",
    "            return False\n",
    "        \n",
    "        # 检查所有元素是否唯一\n",
    "        return len(set(nums)) == n\n",
    "\n",
    "# 测试代码\n",
    "sol = Solution()\n",
    "print(sol.isConsecutive([1, 2, 3, 4, 5]))  # 应输出 True\n",
    "print(sol.isConsecutive([1, 3, 2, 5]))  # 应输出 False\n",
    "print(sol.isConsecutive([5, 6, 7, 8]))  # 应输出 True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        return max(nums) - min(nums) + 1 == len(nums) and len(set(nums)) == len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isConsecutive(self, nums: List[int]) -> bool:\n",
    "        if len(set(nums)) != len(nums):\n",
    "            return False\n",
    "    \n",
    "        return min(nums) + len(nums) - 1 == max(nums)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
